<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Admin\Controller;

use Admin\Api\DatatreeApi;
use Admin\Api\PostApi;
use Admin\Api\VPostInfoApi;
use Admin\Model\DatatreeModel;
use Uclient\Api\UserApi;

class PostController extends  AdminController{

	public function index(){

    $result  = apiCall(DatatreeApi::QUERY_NO_PAGING,array(array('parentid'=>'21')));
    if($result['status']){
        $this->assign("article_cates",$result['info']);
    }


    $name = I("name",'');
    $cate = I("cate",'');

		//分页时带参数get参数
		$params = array(
			'name'=>$name,
		);

		$map = array();

		//$map['post_author'] = UID;
		$map['name'] = array('like', '%'.$name.'%');
		if(!empty($cate)){
			$params['cate'] = $cate;
			$this->assign("cur_cate",$cate);
			//仅查询了下一级子类目
			$result  = apiCall(DatatreeApi::QUERY_NO_PAGING,array(array('parentid'=>$cate)));
			if($result['status'] && $result['info']){
				foreach ($result['info'] as $v) {
					$cate .= ','.$v['id'];
				}
				$map['post_category'] = ['in',$cate];
			}else{
				$map['post_category'] = $cate;
			}

		}

		$page = array('curpage' => I('get.p', 0), 'size' => C('LIST_ROWS'));
		$order = " post_modified desc ";

		$result = apiCall(VPostInfoApi::QUERY,array($map,$page,$order,$params));
		//
		if($result['status']){
			$this->assign('name',$name);
			$this->assign('show',$result['info']['show']);
			$this->assign('list',$result['info']['list']);
			$this->display();
		}else{
			LogRecord('INFO:'.$result['info'],'[FILE] '.__FILE__.' [LINE] '.__LINE__);
			$this->error(L('UNKNOWN_ERR'));
		}
	}


	public function add(){
		if(IS_GET){
			$this->display();
		}else{
			$post_category = I('post.post_category',20);
			$start_time    = I('post.start_time',0);
			$end_time      = I('post.end_time',0);
			$start_time    = strtotime($start_time);
			$end_time      = strtotime($end_time);
			$jump_url      = I('post.jump_url','');
			$user = session("global_user");

			$entity = array(
				'jump_url'       =>$jump_url,
				'start_time'     =>$start_time,
				'end_time'       =>$end_time,
				'main_img'       =>I('post.main_img',''),
				'post_category'  =>$post_category,
				'post_content'   =>I('post_content',''),
				'post_excerpt'   =>I('post_excerpt',''),
				'post_title'     =>I('post_title',''),
				'post_author'    =>UID,
				'post_status'    =>I('post_status','draft'),
				'comment_status' =>I('commen_status','closed'),
				'post_parent'    =>0,
				'post_type'      =>'post_type',
				'comment_count'  =>0
			);

			$entity['post_author_name'] = $user['username'];
			$result = apiCall("Admin/Post/add", array($entity));
			if(!$result['status']){
				$this->error($result['info']);
			}

			$this->success("操作成功！",U('Admin/Post/index'));

		}
	}

	public function edit(){
		$id = I('id',0);
		if(IS_GET){
			$result = apiCall("Admin/Post/getInfo", array(array("id"=>$id)));
			if(!$result['info']){
				$this->error($result['info']);
			}
			$this->assign("vo",$result['info']);
			$this->display();
		}else{
			$post_category = I('post.post_category',20);
			$start_time = I('post.start_time','');
			$end_time = I('post.end_time','');
			$start_time = strtotime($start_time);
			$end_time = strtotime($end_time);
			$user = session("global_user");
			$jump_url = I('post.jump_url','');

			$entity = array(
				'jump_url'=>$jump_url,
				'start_time'=>$start_time,
				'end_time'=>$end_time,
				'main_img'=>I('post.main_img',''),
				'post_category'=>$post_category,
				'post_author'=>UID,
				'post_content'=>I('post_content',''),
				'post_excerpt'=>I('post_excerpt',''),
				'post_title'=>I('post_title',''),
				'post_status'=>I('post_status','draft'),
				'comment_status'=>I('commen_status','closed'),
			);
			$entity['post_author_name'] = $user['username'];
			$result = apiCall(PostApi::SAVE_BY_ID, array($id,$entity));

			if(!$result['status']){
				$this->error($result['info']);
			}

			$this->success("保存成功！",U('Admin/Post/index'));

		}
	}

	public function delete(){
		$id = I('id',0);

		$result = apiCall("Admin/Post/delete", array(array("id"=>$id)));

		if(!$result['status']){
			$this->error($result['info']);
		}

		$this->success("删除成功！");

	}

}
